function IntegralImage(input) {
  this.mIntImage = new Array(input.width);
  for (var i = 0; i < input.width; i++) {
    this.mIntImage[i] = new Array(input.height);
  }
  this.mWidth = this.mIntImage.length;
  this.mHeight = this.mIntImage[0].length;
  var width = input.width;
  var height = input.height;

  var sum,
    index = 0,
    intensity;
  for (var y = 0; y < height; y++) {
    sum = 0;
    for (var x = 0; x < width; x++) {
      /**
       * TODO: FIX LOSS IN PRECISION HERE, DON'T ROUND BEFORE THE DIVISION (OR AFTER, OR AT ALL)
       * This was done to match the C++ version, can be removed after confident that it's working
       * correctly.
       */
      intensity = Math.round((0.299 * input.data[index] + 0.587 * input.data[index + 1] + 0.114 * input.data[index + 2])) / 255.0;
      sum += intensity;
      if (y == 0) {
        this.mIntImage[x][y] = sum;
      } else {
        this.mIntImage[x][y] = sum + this.mIntImage[x][y - 1];
      }
      index += 4;
    }
  }

  this.getWidth = function () {
    return this.mWidth;
  }

  this.getHeight = function () {
    return this.mHeight;
  }

  this.getValue = function (column, row) {
    return this.mIntImage[column][row];
  }
}

module.exports = IntegralImage
